package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.excption.ArchiveException;
import zhongbo.java.air.dictionary.excption.ExceptionCode;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class AirCraftServiceImpl implements IAirCraftService {

    @Autowired
    private AirCraftMapper airCraftMapper;


    @Override
    public AirCraft add(AirCraft craft) {
        validAirCraft(craft);
        craft.setCreateTime(new Date());
        craft.setUpdateTime(new Date());
        int insert = airCraftMapper.insert(craft);
        if (insert > 0) {
            return craft;
        }
        throw new RuntimeException("新增飞机失败");
    }

    /**
     * 验证信息
     *
     * @param craft 飞机实体
     */
    private void validAirCraft(AirCraft craft) {
        if (craft == null) {
            throw new RuntimeException("飞机信息不能为空");
        }
        List<AirCraft> airCrafts = airCraftMapper.getAll();
        String registration = craft.getRegistration();
        if (StrUtil.isBlank(registration)) {
            throw new RuntimeException("飞机注册号不能为空");
        }
        for (AirCraft airCraft : airCrafts) {
            if (Objects.equals(registration, airCraft.getRegistration())) {
                throw new RuntimeException("飞机注册号不能重复");
            }
        }
        String airline = craft.getAirline();
        if (StrUtil.isBlank(airline)) {
            throw new RuntimeException("飞机航空公司不能为空");
        }
        String code = craft.getIcaoCode();
        if (code.length() > 6) {
            throw new RuntimeException("ICAO不能不能超过6个字段");
        }

        for (AirCraft airCraft : airCrafts) {

            if (Objects.equals(code, airCraft.getIcaoCode())) {
                throw new ArchiveException(ExceptionCode.UNIQUE_FIELD);
                //              throw new RuntimeException("ICAO唯一的");
            }
        }
//        ACStatus status = craft.getStatus();
//        System.out.println("status"+status);
//        if (status != ACStatus.ACTIVE && status != ACStatus.MAINTENANCE && status != ACStatus.RETIRED) {
//            throw new RuntimeException("只能是枚举值：ACTIVE-运营中, MAINTENANCE-维护中, RETIRED-已退役");
//        }
    }

    @Override
    public String delete(String id) {

        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        int delete = airCraftMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new ArchiveException(ExceptionCode.CANNOT_BE_DELETE);

    }

    @Override
    public AirCraft edit(AirCraft craft1) {
        validAirCraft(craft1);
        String id = craft1.getId();
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        AirCraft craft = airCraftMapper.selectById(id);
        if (craft == null) {
            throw new ArchiveException(ExceptionCode.CANNOT_BE_MODIFIED);
        }
        int update = airCraftMapper.updateById(craft1);
        craft1.setUpdateTime(new Date());
        if (update > 0) {
            return craft1;
        }
        throw new RuntimeException("修改飞机失败");

    }

    @Override
    public AirCraft findOne(String id) {
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        AirCraft craft = airCraftMapper.selectById(id);
        if (craft == null) {
            throw new ArchiveException(ExceptionCode.CANNOT_BE_QUERY);
        } else {
            return craft;
        }
    }

    @Override
    public PageVo<AirCraft> findPage(String keyword, String fields,
                                     Integer pageNumber, Integer pageSize,
                                     String oderByField, Boolean oderByAsc) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        //支持指定字段模糊搜索Keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            //检测是正序还是倒序
            boolean isOderByAsc = Boolean.TRUE.equals(oderByAsc);
            if (isOderByAsc) {
                //正
                queryWrapper.orderByAsc(oderByField);
            } else {
                //倒序
                queryWrapper.orderByAsc(oderByField);
            }
        }
        Page<AirCraft> pageParam = new Page<>(pageNumber, pageSize);
        Page<AirCraft> airPage = airCraftMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(airPage);

    }
}
